package controllers

import (
	"github.com/astaxie/beego"
	"demo/models"
	"fmt"
)



type UserController struct {
	beego.Controller
}

func (c *UserController) Get() {
	//插入数据
	// user := models.User{Name:"zhangsan",Age:30,Gender:1}
	// id, err := models.AddUser(&user) 
	// if err != nil{
	// 	c.Ctx.WriteString("error")
	// }
	// c.Ctx.WriteString(fmt.Sprintf("success %v",id))


	/**
	* 分页查询
	**/
	page,err := strconv.Atoi(c.Input().Get("p")) /*获取页码 并转化成整形*/
	if err != nil {  
		c.Ctx.WriteString("params is error")
	}
	if page <= 1 { 
		page = 1
	}
	//
	limit := 2 /*每页两条*/
	offset := (page -1) * limit /*跳过多少数据查询*/

	var users []models.User 
	models.GetUsers(&users,limit,offset) /*查询操作*/
	//转化json
	data, err := json.Marshal(users)
    if err != nil {
    	c.Ctx.WriteString(fmt.Sprintf("json.marshal failed, err:%s", err))
        return
    }


	
}

